﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Common;

namespace CNCTools
{
    public partial class PanelToolsForm : Form
    {

        private readonly List<Part> _parts = new List<Part>();
        
        private const float PanelXStart = 500;
        private const float PanelYStart = 25;

        private float _panelSizeX;
        private float _panelSizeY;

        private new const int Scale = 10;
        
        private float _currentPartX; 
        private float _currentPartY;
        private float _lowestYPoint = 2000;
        private float _actualXSize;
        private float _actualYSize;
        private float _spacing;


        public PanelToolsForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (var file in openFileDialog1.FileNames)
                {
                    var newPart = new Part(file);
                    newPart.PartID = _parts.Count + 1;
                    _parts.Add(newPart);
                    filesListBox.Items.Add(newPart.Filename);    
                }
                
            }

            label9.Text = "Files Loaded: " + filesListBox.Items.Count;
        }


        public void AddPart()
        {
            // maybe find the part that has the same filename and display that data?
            Part currentPart = _parts.Find(c => c.Filename == filesListBox.SelectedItem.ToString());

            smallXLabel.Text = "Smallest X Value: " + currentPart.SmallestX;
            smallYLabel.Text = "Smallest Y Value: " + currentPart.SmallestY;
            largeXlabel.Text = "Largest X Value: " + currentPart.LargestX;
            largeYLabel.Text = "Largest Y Value: " + currentPart.LargestY;
            xSizeLabel.Text = "X Size: " + currentPart.XSize;
            ySizeLabel.Text = "Y Size: " + currentPart.YSize;
            
            _actualXSize = currentPart.XSize * Scale;
            _actualYSize = currentPart.YSize * Scale;

            // If the current position plus the part size is greater than the size of the panel in X...
            if (_currentPartX + _actualXSize > _panelSizeX + PanelXStart)
            {
                // reset the _currentpartX back to the panel start...
                _currentPartX = PanelXStart;

                // and push the _currentPartY to the next row...
                _currentPartY = _lowestYPoint - _spacing;
            }

            // If the current position plus the part size is greater than the size of the panel in Y...
            //if (_currentPartY + _actualYSize > _panelSizeY + PanelYStart)
            //{
            //    MessageBox.Show("There is no more room for a part of that height...");
            // }
            //else
            {
                float actualXpoint = _currentPartX;
                float actualYPoint = _currentPartY - (currentPart.YSize*Scale);

               // currentPart.OffsetGCodeLines(actualXpoint, actualYPoint);

                // Draw the Outline...
                var partView = CreateGraphics();
                var pen = new Pen(Color.Red, 2);

                partView.DrawRectangle(pen, actualXpoint, actualYPoint, _actualXSize, _actualYSize);

                // Move the _currentPartX to the next spot...
                _currentPartX = _currentPartX + _actualXSize + _spacing;

                // Keep track of the lowest point on the Y row....
                if (actualYPoint < _lowestYPoint)
                {
                    _lowestYPoint = actualYPoint;
                }
            }

        }


        private void resetButton_Click(object sender, EventArgs e)
        {
            // Clear the screen and files? or just the screen?


        }

        private void setPanelButton_Click(object sender, EventArgs e)
        {
            _panelSizeX = (float)(Convert.ToDecimal(panelSizeXTextBox.Text)) * Scale;
            _panelSizeY = (float)(Convert.ToDecimal(panelSizeYTextBox.Text)) * Scale;

            _currentPartX = PanelXStart;
            _currentPartY = PanelYStart + _panelSizeY;

            _spacing = (float)Convert.ToDouble(spacingTextBox.Text);

            // Draw the Panel... Scale by 10...
            var g = CreateGraphics();
            var pen = new Pen(Color.Black, 2);
            g.DrawRectangle(pen, PanelXStart, PanelYStart, _panelSizeX, _panelSizeY);
        }

        private void filesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Display the Part Info...
            Part currentPart = _parts.Find(c => c.Filename == filesListBox.SelectedItem.ToString());

            smallXLabel.Text = "Smallest X Value: " + currentPart.SmallestX;
            smallYLabel.Text = "Smallest Y Value: " + currentPart.SmallestY;
            largeXlabel.Text = "Largest X Value: " + currentPart.LargestX;
            largeYLabel.Text = "Largest Y Value: " + currentPart.LargestY;
            xSizeLabel.Text = "X Size: " + currentPart.XSize;
            ySizeLabel.Text = "Y Size: " + currentPart.YSize;
        }
    }
}
